package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.dao.model.RoleMenu;
import com.woniuxy.dao.RoleMenuDao;
import com.woniuxy.service.RoleMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author psy
 * @since 2022-06-28
 */
@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuDao, RoleMenu> implements RoleMenuService {

    @Resource
    private RoleMenuDao roleMenuDao;

    /**
     * 超级管理员给角色授权相应菜单的接口
     * @param roleId
     * @param menuIds
     */
    @Override
    public void addAuthorization(Integer roleId, int[] menuIds) {
        //1.先删除角色对应的所有菜单
        roleMenuDao.delete(new QueryWrapper<RoleMenu>().eq("roleId",roleId));

        //2.再添加角色所勾选的所有菜单
        for(int i = 0; i<menuIds.length; i++){

            RoleMenu roleMenu = new RoleMenu();

            roleMenu.setRoleId(roleId);

            roleMenu.setMenuId(menuIds[i]);

            roleMenuDao.insert(roleMenu);
        }


    }
}
